#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django.db import models

# Create your models here.


# 任务表
class TaskModel(models.Model):
    status = (
        (0, '未运行'),
        (1, '运行中'),
        (2, '已完成')
    )
    title = models.CharField(max_length=100, verbose_name='任务标题')
    task_type = models.ForeignKey('testcase.SpeakerProduct', on_delete=models.CASCADE, verbose_name='任务类型')
    test_speaker = models.ForeignKey('speaker.SpeakerStatus', on_delete=models.CASCADE, default=None, null=True, verbose_name='临时测试音箱')
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    is_smoke = models.BooleanField(default=None, verbose_name='是否为冒烟用例')
    creator = models.ForeignKey('user.User', on_delete=models.CASCADE, verbose_name='任务创建人员')
    start_time = models.DateTimeField(verbose_name='任务开始时间', null=True, blank=True)
    end_time = models.DateTimeField(verbose_name='任务结束时间', null=True, blank=True)
    pass_rate = models.IntegerField(verbose_name='用例通过率', null=True, blank=True, default=0)
    progress = models.IntegerField(verbose_name='任务进度', null=True, blank=True, default=0)
    desc = models.CharField(max_length=500, null=True, blank=True, default='', verbose_name='任务简介')
    status = models.SmallIntegerField(choices=status, default=0, verbose_name="任务状态")

    class Meta:
        db_table = 'task'
        verbose_name = '任务表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class TaskModuleModel(models.Model):
    task = models.ForeignKey('TaskModel', on_delete=models.CASCADE, verbose_name='所属任务')
    module = models.ForeignKey('testcase.CaseModule', on_delete=models.CASCADE, verbose_name='模块id')

    class Meta:
        db_table = 'task_module'
        verbose_name = '任务-模块表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.module.module_name


# 任务用例详情表
class TaskCaseModel(models.Model):
    task_module = models.ForeignKey('TaskModuleModel', on_delete=models.CASCADE, verbose_name='所属任务用例模块')
    test_case = models.ForeignKey('testcase.TestCase', on_delete=models.CASCADE, verbose_name='测试用例')
    task = models.ForeignKey('TaskModel', on_delete=models.CASCADE, verbose_name='所属任务')
    is_success = models.BooleanField(default=False, verbose_name='是否通过')
    fail_detail = models.CharField(max_length=500, default='', verbose_name='失败日志')
    success_detail = models.CharField(max_length=500, default='', verbose_name='成功日志')
    runs = models.IntegerField(default=0, verbose_name='重试次数')

    class Meta:
        db_table = 'task_case'
        verbose_name = '任务用例详情表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.test_case.case_query
